package com.keta.foundation.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.keta.foundation.dataFilter.DynamicSpecifications;
import com.keta.foundation.entity.LogInfo;
import com.keta.foundation.log.Log;
import com.keta.foundation.log.LogUtil;
import com.keta.foundation.service.LogInfoService;
import com.keta.foundation.util.dwz.AjaxObject;
import com.keta.foundation.util.dwz.Page;

/**
 * 日志控制器 管理系统审计日志，凡是对数据库进行 增 删 改 都需要记录审计日志， 以便追踪
 * 
 * @author robin
 */
@Controller
@RequestMapping("/logInfo")
public class LogInfoController {

	private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

	private static final String LOG_SHOW = "logInfo/list";

	@Autowired
	private LogInfoService logInfoService;

	/**
	 * 日志列表入口
	 */
	@RequiresPermissions("LogInfo:view")
	@RequestMapping("/list")
	public String list(ServletRequest request, Page page, Model model) {
		Specification<LogInfo> specification = DynamicSpecifications.byDataFilter(request, LogInfo.class);
		page.setOrderDirection(Direction.DESC.toString());
		page.setOrderField("createTime");
		List<LogInfo> logInfoList = logInfoService.getAll(specification, page);
		model.addAttribute("logInfoList", logInfoList);
		model.addAttribute("page", page);

		return LOG_SHOW;
	}

	/*
	 * 删除日志
	 */
	@Log(message = "删除了{0}条日志")
	@RequiresPermissions("LogInfo:delete")
	@RequestMapping("/delete")
	public @ResponseBody String delete(Long[] ids) {

		for (Long id : ids) {
			logInfoService.delete(id);
		}
		
		LogUtil.putArgs(new Object[] { ids.length });
		logger.info("成功删除了" + ids.length + "条日志！");
		return AjaxObject.newOk("成功删除了" + ids.length + "条日志！").setCallbackType("").toString();
	}

}
